<template>
  <div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" style="width: 80%">
      <el-form-item label="手机号" prop="phone" >
        <el-input v-model="ruleForm.phone" style="width: 50%; margin-right: 20px;"></el-input><el-button type="primary" @click="codeToPhone" :disabled="codeBtnDis">{{msg}}</el-button>
      </el-form-item>
      <el-form-item label="验证码" prop="code">
        <el-input v-model="ruleForm.code" style="width: 50%; margin-right: 20px;"></el-input>
        <span style="color: red">验证码: 6个1, 省钱, 上线时再开放</span>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login('ruleForm')" style="width: 20%; margin: 0 auto">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      msg: '获取短信验证码',
      codeBtnDis: false,
      ruleForm: {
        phone: "",
        code: ""
      },
      rules: {
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { min: 11, max: 11, message: '手机号为11位', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { min: 6, max: 6, message: '验证码为6位', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    codeToPhone(formName) {
      if (!this.ruleForm.phone || this.ruleForm.phone.length != 11) {
        this.$message({
          message: '请输入正确的手机号!!!',
          type: 'error'
        });
        return;
      }
      this.$http.get("/user/codeToPhone?phone="+this.ruleForm.phone).then(res => {
        res = res.data;
        if (res.code === 0) {
          this.$message({
            message: '已发送至您的手机, 请注意查收!',
            type: 'success'
          });
          this.codeBtnDis = true;
          this.msg = "60s"
          let second = 60;
          let _this = this;
          let i = setInterval(function () {
            console.log(second)
            if (second <= 0) {
              this.codeBtnDis = false
              _this.msg = "重新获取短信验证码"
              clearInterval(i);
              return;
            }
            second--;
            _this.msg = second + "s";
            console.log(_this.msg)
          },1000)
        }
      })


    },
    login(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$http.post("/user/login", this.ruleForm).then(res => {
            res = res.data;
            if (res.code === 0) {
              this.$message({
                message: '登录成功',
                type: 'success'
              });
              // 用户信息
              let userInfo = res.data
              this.$store.commit('setUserInfo', userInfo)
              // 跳转到首页
              this.$router.push({ path: `/blog/person/list`})
            } else {
              this.$message({
                message: '登录失败',
                type: 'warning'
              });
            }
          }).catch((error) => {
            console.log(error);
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>

</style>